﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using IFPRJ.EF.Components.SkinEgine;
using System.IO;

namespace IFPRJ.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index" ,id = UrlParameter.Optional } // 参数默认值
                , new string[] { "MvcFckEditor.Controllers" }

            );

        }

        protected void Application_Start()
        {
            // 使用自定义的皮肤视图引擎 SkinViewEngine
            // 对应的视图文件夹为：/Skins/皮肤名称/控制器名称/
            // 设置皮肤：HttpContext.SetSkinName(skin);
            // 获取皮肤：string skinName = HttpContext.GetSkinName();
            
            

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new IFPRJ.EF.Components.IFWebRazorViewEngine(GetSkins()));  
            //“~/skins/default/Home/Index.cshtml”处的视图必须派生自 ViewPage、ViewPage<TModel>、ViewUserControl 或 ViewUserControl<TModel>。 
            //换肤后 总是报这个错，不知是不是 RAZOR的视图 不能继承WEBFORMVIEW
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            
        }

        public static string GetSkins()
        {
            string skinName;
            if (HttpContext.Current.Items["skinname"] == null)
            {
                skinName = "Default";
                HttpContext.Current.Items.Add("skinname", skinName);
            }
            else
            {
                skinName = HttpContext.Current.Items["skinname"].ToString();
            }
            return skinName;
        }
    }
}